package com.qf.anno.web.controller;

import com.qf.anno.domain.R;
import com.qf.anno.domain.User;
import com.qf.anno.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public R login(@RequestBody User user, HttpServletRequest req){
        User loginUser = userService.login(user);

        if (loginUser != null){
            //登录成功 - ① 存入session ② 响应页面数据
            req.getSession().setAttribute("userInfo",loginUser);

            return R.success("登录成功");
        }

        //登录失败
        return R.error("用户名或密码错误");
    }
}
